//
// Created by lights on 2024/6/22.
//

#ifndef ALGORITHM_COMMON_H
#define ALGORITHM_COMMON_H

#endif //ALGORITHM_COMMON_H

#include <iostream>
#include "vector"
#include "map"
#include "stack"
#include "string"
#include "string.h"
#include <fstream> // 文件操作需要包含的头文件
#include <streambuf>
#include <chrono>
#include <ctime>
#include <thread>
#include <iomanip>

class LoggerStreamBuf : public std::streambuf {
public:
    LoggerStreamBuf(std::streambuf* buf, std::ostream& log_stream)
            : original_buf(buf), log_stream(log_stream) {}

protected:
    virtual int overflow(int c) override {
        if (c != EOF) {
            char z = c;
            // 输出到原始缓冲区（终端）
            original_buf->sputc(z);
            // 输出到日志文件
            log_stream.put(z);
        }
        return c;
    }

    virtual std::streamsize xsputn(const char* s, std::streamsize n) override {
        // 输出到原始缓冲区（终端）
        original_buf->sputn(s, n);
        // 输出到日志文件
        log_stream.write(s, n);
        return n;
    }

private:
    std::streambuf* original_buf;
    std::ostream& log_stream;
};

